﻿using System;
using System.Windows;
using System.Windows.Controls;
using SABnzbdNET.Dialogs;
using SABnzbdNET.ServiceAgents;

namespace SABnzbdNET.Views
{
    /// <summary>
    /// Interaction logic for Toolbar.xaml
    /// </summary>
    public partial class Toolbar : UserControl
    {
        public Toolbar()
        {
            InitializeComponent();
        }

        private void BtnNew_Click(object sender, RoutedEventArgs e)
        {
            var add = new AddTaskDialog() { Owner = Application.Current.MainWindow };
            add.ShowDialog();
        }

        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            SetStatusMessage();
            new ServerAgent().StartServer(UpdateServerInfo);
        }

        private void BtnStop_Click(object sender, RoutedEventArgs e)
        {
            SetStatusMessage();
            new ServerAgent().PauseServer(UpdateServerInfo);
        }

        private void BtnShutDown_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Are You sure You want to shut down SABnzbd?",
                "Shut down SABnzbd", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                SetStatusMessage();
                new ServerAgent().ShutDownServer(UpdateServerInfo);
            }
        }

        private void BtnSettings_Click(object sender, RoutedEventArgs e)
        {
            var set = new SettingsDialog {Owner = Application.Current.MainWindow};
            set.ShowDialog();
        }

        private void BtnInfo_Click(object sender, RoutedEventArgs e)
        {
            var about = new AboutDialog { Owner = Application.Current.MainWindow };
            about.ShowDialog();
        }

        private void BtnRefresh_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.MainWindow is Main)
                ((Main)Application.Current.MainWindow).UpdateServerStatus();
        }

        private void SetStatusMessage()
        {
            if (Application.Current.MainWindow is Main)
            {
                ((Main)Application.Current.MainWindow).StatusMessage = "Updating";
                ((Main)Application.Current.MainWindow).NotifyIconTooltip = "Updating";
            }
        }

        private void UpdateServerInfo()
        {
            if (Application.Current.MainWindow is Main)
                ((Main)Application.Current.MainWindow).UpdateServerStatus();
        }
    }
}
